import { _decorator, Button, Component, EditBox, instantiate, Node, Prefab, Widget } from 'cc';
import { UIComponent } from '../../platform/ui/UIComponent';
import { bind } from '../../platform/ui/UIDecorator';
import { ResManager } from '../../platform/resources/ResMgr';
import { TbRoot } from '../../platform/excel_table/TbRoot';
import { Tb_Monster, Tb_MonsterContainer } from '../tb/TB_Monster';
import { LogMgr } from '../../platform/log/LogMgr';
import { BattleRoom } from './BattleRoom';
import Monster from './Monster';
import { Tb_HeroContainer } from '../tb/TB_Hero';
import Hero from './Hero';
const { ccclass, property } = _decorator;

@ccclass('HomeTest')
export class HomeTest extends UIComponent {
    @bind(Node)
    private game;
    @bind(Node)
    private gm_ctl;
    @bind(Node)
    private gm_ctl_toggle;

    @bind(Node)
    private monsters;

    @bind(Node)
    private uis;

    @bind(EditBox)
    private monsterId;
    @bind(Button)
    private addMonsterBtn;

    @bind(EditBox)
    private heroId;
    @bind(Button)
    private addHeroBtn;

    nowMonsterId = 1;

    protected start(): void {
        this.gm_ctl_toggle.on(Button.EventType.CLICK, this.ctlToggle, this);
        this.addMonsterBtn.node.on(Button.EventType.CLICK, this.onAddMonster, this);
        this.addHeroBtn.node.on(Button.EventType.CLICK, this.onAddHero, this);
        this.getComponent(Widget).updateAlignment();
        BattleRoom.newRoom()
        BattleRoom.currentRoom.reset(1)
        BattleRoom.currentRoom.start()
    }

    protected update(dt: number): void {
        let room =  BattleRoom.currentRoom
        if(room && room.status==1){
            //先检测敌人数量，是否自动生成
            if(room.monsters.length<3){
                this.createMonster(this.nowMonsterId) //默认生成id为1的怪物
                this.nowMonsterId++;
                this.nowMonsterId = this.nowMonsterId>3?1:this.nowMonsterId; //循环生成1-3的怪物
            }
            room.update(dt)
        }
    }

    ctlToggle(){
        let old = this.gm_ctl.active
        if(old){
            this.gm_ctl.active = false
            return
        }
        this.gm_ctl.active = true

    }
    onAddHero(){
        let heroId = this.heroId.string
        if(heroId){
            this.createHero(Number(heroId))
        }else{
            console.error("hero is null")
        }
    }
    onAddMonster(){
        let monsterId = this.monsterId.string
        if(monsterId){
            this.createMonster(Number(monsterId))
        }else{
            console.error("monsterId is null")
        }
    }
    createMonster(monsterId: number){
        let monsterContainer = TbRoot.ins.getTbContainer(Tb_MonsterContainer)
        let cfg = monsterContainer.getCfgById(monsterId)
        if(!cfg){
            LogMgr.err("monster cfg is null，id:"+monsterId.toString())
            return
        }
        ResManager.inst.loadBundleRes("resources/prefab/monster",Prefab).then((prefab: Prefab) => {
            let monsterNode = instantiate(prefab);
            monsterNode.name = monsterId.toString();
            let cmp = monsterNode.addComponent(Monster);
            cmp.cfgId = monsterId;
            monsterNode.setParent(this.monsters);
            BattleRoom.currentRoom.addMonsters(cmp);
            // monsterNode.getComponent(Monster).initMonster(monsterId);
            //添加monster脚本
            return monsterNode;
        }).catch((err) => {
            console.error("load monster prefab error", err);
        })
    }

    createHero(heroId: number){
        let heroContainer = TbRoot.ins.getTbContainer(Tb_HeroContainer)
        let cfg = heroContainer.getCfgById(heroId)
        if(!cfg){
            LogMgr.err("hero cfg is null，id:"+heroId.toString())
            return
        }
        ResManager.inst.loadBundleRes("resources/prefab/hero",Prefab).then((prefab: Prefab) => {
            let heroNode = instantiate(prefab);
            heroNode.name = heroId.toString();
            let cmp = heroNode.addComponent(Hero);
            heroNode.setParent(this.monsters);
            BattleRoom.currentRoom.addHero(cmp);
            return heroNode;
        }).catch((err) => {
            console.error("load hero prefab error", err);
        })
    }
}


